package
{
import away3d.cameras.*;
import away3d.core.math.*;
import away3d.core.scene.*;
import away3d.loaders.*;

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent; 
	public class HoverCamera3d extends Sprite
	{
		var rotCamera:Boolean = false;
		var lastMouseX:Number = mouseX;
		var lastMouseY:Number = mouseY;
		var firstClick:Boolean = true;
		var hit:MovieClip = new MovieClip();
		var view:View3D;
		var camera;
		var loader:Object3DLoader;
		
		[Embed(source="images/Material1.jpg")]
		public var clockFace:Class;
		public function HoverCamera3d()
		{
			camera = new HoverCamera3D({zoom:1, focus:200, distance:1000});
			// create a 3D-viewport
			camera.tiltangle = 40;
			camera.targettiltangle = 40;
			camera.mintiltangle =  20;
			camera.maxtiltangle = 50;
			camera.yfactor = 1;
			camera.steps = 7;
			
			view = new View3D({camera:camera, x:300, y:200});
			// add viewport to the stage
			addChild(view); 
			
			// start mesh loading
//			loader = Obj.load("resources/turtle.obj",
//			{material:"turtle_texture", name:"turtle", scaling:0.1, y:50, x:10, z:
//			10, loadersize:300});
			loader = 
//			Collada.load("D:/p3dworkspace/Away3dLib/src/models/dormitorio futuro.dae");
			Collada.load("models/bradleycollectionconsole.dae",{materials:{Material1ID:clockFace}});
			loader.addOnSuccess(addToScene); 
			
			loader.scale(5);
//			loader.moveBackward(1500);
			
			
			// add the loader object to the scene
//			view.scene.addChild(loader); 
			
			camera.update();
			loader.rotationX = -90;
			//
			
			
			//
			addEventListener(Event.ENTER_FRAME, onEnterFrame); 
			
//			hit.graphics.beginFill(0xffffff);
//			hit.graphics.drawRect(0, 0, 500, 600);
//			hit.graphics.endFill();
//			addChild(hit);
//			hit.alpha = 0;
//			hit.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
//			hit.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
//			hit.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseEvent);

		}
		function addToScene(event:Event)
		{
			trace("------------------------------------------");
		    view.scene.addChild(loader.handle);
		
		} 
 private function initJeep(event:Event):void
        {
        	
			loader.handle.pitch(10);
        }
 	function onEnterFrame(event:Event):void {
        //
        if (rotCamera) {
                if (firstClick == true) {
                        firstClick = false;
                        lastMouseX = view.mouseX;
                        lastMouseY = view.mouseY;
                        trace("frist click");
                }
                //
                var dragX = (view.mouseX - lastMouseX);
                var dragY = (view.mouseY - lastMouseY);
                //
                lastMouseX = view.mouseX;
                lastMouseY = view.mouseY;
                //
                camera.targetpanangle += dragX;
                camera.targettiltangle += dragY
                camera.hover();
        } else {
                //
        }
        // rerender viewport on each frame
        view.render();
	}
	
	function mouseDownHandler(evt:MouseEvent):void {
        rotCamera = true;
}


function mouseUpHandler(evt:MouseEvent):void {
        rotCamera = false;
        firstClick = true;
}

function onMouseEvent(event:MouseEvent):void {
        trace(camera.zoom);
        var dir = (event.delta > 0) ? 0.5 : -0.5;
        camera.zoom = Math.max(2, Math.min(8, (camera.zoom+dir)));
        //camera.distanceTo(-40); 
	 
	}
	}
}